package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.activity.NewActivityPlaceInfo;
import com.audi.dto.SceneItem;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;
import com.audi.utils.Constants;
import com.audi.utils.SceneHelper;
import com.audi.view.AudiDialog;
import com.audi.view.BaseDeviceDialog;
import com.audi.view.BaseSceneDialog;
import com.audi.view.DeviceUpdatePhoto;

public class SceneAdapter extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<SceneItem> mSceneList;
	private AudiDialog mDialogRename;
	private AudiDialog mDialogColorDetail;
	private DeviceUpdatePhoto mDeviceUpdatePhoto;
	private AlertDialog.Builder mListDialog;
	private int mIndexSceneList;
	private SendMessage sendMessage;
	private SQLiteAsusNewHelper mSQLHelper;

	public SceneAdapter(Activity activity, ArrayList<SceneItem> sceneList,
			DeviceUpdatePhoto deviceUpdatePhoto) {
		mActivity = activity;
		mSceneList = sceneList;
		mDeviceUpdatePhoto = deviceUpdatePhoto;
		sendMessage = new SendMessage();
	}

	public void setDataForAdapterScene(
			ArrayList<SceneItem> sceneListInRootAccess) {
		this.mSceneList = sceneListInRootAccess;
		this.notifyDataSetChanged();

	}

	public SceneAdapter(Activity activity, ArrayList<SceneItem> sceneList) {
		this.mActivity = activity;
		this.mSceneList = sceneList;
	}

	public SceneAdapter() {
		super();
	}

	public int getCount() {
		return mSceneList.size();
	}

	public SceneItem getItem(int pos) {
		return mSceneList.get(pos);
	}

	public long getItemId(int positonScene) {
		return positonScene;
	}

	@SuppressLint("InflateParams")
	public View getView(int positionScene, View convertView, ViewGroup viewgroup) {
		final SceneItem sceneItem = mSceneList.get(positionScene);
		ViewHolder viewHolderScene = null;
		if (convertView == null) {
			viewHolderScene = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_scene_card, null);
			viewHolderScene.rltDrag = (RelativeLayout) convertView
					.findViewById(R.id.rlt_drag_scene);
			viewHolderScene.lnlSceneContainer = (RelativeLayout) convertView
					.findViewById(R.id.lnl_snece_container);
			viewHolderScene.imvSceneIcon = (ImageView) convertView
					.findViewById(R.id.imv_scene_icon);
			viewHolderScene.txtSceneTitleName = (TextView) convertView
					.findViewById(R.id.txt_scene_title_name);
			viewHolderScene.imvSceneMenu = (ImageView) convertView
					.findViewById(R.id.imv_scene_menu);
			convertView.setTag(viewHolderScene);
		} else {
			viewHolderScene = (ViewHolder) convertView.getTag();
		}

		// set Data for View

		viewHolderScene.txtSceneTitleName.setText(sceneItem.getSceneTitle());

		/*
		 * if (TextUtils.isEmpty(sceneItem.getSceneIcon().trim())) {
		 * viewHolderScene.imvSceneIcon
		 * .setImageResource(R.drawable.scene_movie); } else { BitmapDrawable
		 * bitmapDrawable = new BitmapDrawable( mActivity.getResources(),
		 * sceneItem.getSceneIcon()); Bitmap bitmap=
		 * setImage(sceneItem.getSceneIcon());
		 * viewHolderScene.imvSceneIcon.setImageDrawable(bitmap); }
		 */
		// Set item cho ListView.
		if (positionScene == 0) {
			viewHolderScene.imvSceneIcon
					.setImageResource(R.drawable.scene_movie);
		}
		if (positionScene == 1) {
			viewHolderScene.imvSceneIcon
					.setImageResource(R.drawable.scene_reading);
		}
		if (positionScene == 2) {
			viewHolderScene.imvSceneIcon
					.setImageResource(R.drawable.scene_morning);
		}
		// set Listener for View
		viewHolderScene.imvSceneMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View imvDeviceMenu) {
				// mIndexSceneList = mSceneList.indexOf(item);
				PopupMenu popupMenuScene = new PopupMenu(mActivity,
						imvDeviceMenu);
				popupMenuScene.getMenuInflater().inflate(
						R.menu.popup_menu_sub_scene_card,
						popupMenuScene.getMenu());
				popupMenuScene
						.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
							public boolean onMenuItemClick(MenuItem item) {
								if (item.getItemId() == R.id.item_update_photo) {
									// mDeviceUpdatePhoto.updatePhotoTitle();
									// Code update photo
								}
								if (item.getItemId() == R.id.item_rename) {
									onRenameScene(sceneItem);
								}
								if (item.getItemId() == R.id.item_remove_scene) {
									onRemoveScene(sceneItem);
								}
								return true;
							}
						});
				popupMenuScene.show();
			}
		});
		return convertView;
	}

	static class ViewHolder {
		private RelativeLayout rltDrag;
		private ImageView imvSceneIcon;
		private ImageView imvSceneMenu;
		private TextView txtSceneTitleName;
		private RelativeLayout lnlSceneContainer;
	}

	private void onRenameScene(SceneItem sceneItem) {
		BaseSceneDialog renameSceneDialog = new BaseSceneDialog(mActivity);
		renameSceneDialog.editNameSence(sceneItem, handlerRenameScene);
	}

	private void onRemoveScene(final SceneItem sceneItem) {
		BaseSceneDialog removeSceneDialog = new BaseSceneDialog(mActivity);
		removeSceneDialog.onRemoveSceneDialog(sceneItem, handlerRemoveScene);
		
	}

	@SuppressLint("HandlerLeak")
	private Handler handlerRenameScene = new Handler() {

		public void handleMessage(final Message msg) {
			mActivity.runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							if (msg.getData().getInt(
									String.valueOf(ActionMessage.UPDATE_SCENE)) > 0) {
								sendMessage
										.sendData(
												ActionMessage.GET_LIST_SCENE,
												null,
												((NewActivityPlaceInfo) mActivity).loadListSceneHandlerRootAccess);
							}
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
	@SuppressLint("HandlerLeak")
	private Handler handlerRemoveScene = new Handler() {

		public void handleMessage(final Message msg) {
			mActivity.runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							if (msg.getData().getInt(
									String.valueOf(ActionMessage.DELETE_SCENE)) > 0) {
								sendMessage
										.sendData(
												ActionMessage.GET_LIST_SCENE,
												null,
												((NewActivityPlaceInfo) mActivity).loadListSceneHandlerRootAccess);
							}
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
}